package wangxiaotao.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import wangxiaotao.bean.vo.OrderSimpleVO;
import wangxiaotao.common.CommonResult;
import wangxiaotao.entity.Order;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/admin/dashboard")
@RequiredArgsConstructor
@RequiresRoles(value = {"sadmin","admin","manager","people","clean","service","front"},logical = Logical.OR)
public class DashBoardController extends BaseController{

    /**
     * 获取所有 订单数量 房间数量 用户数量 总金额
     * @return
     */
    @GetMapping("/total")
    public CommonResult getTotalData(){
        return CommonResult.success(adminService.getTotalData());
    }

    /**
     * 按照指定条数 排序方式获取订单数据 dashboard使用
     * @param orderBy
     * @param size
     * @return
     */
    @GetMapping("/orderSimpleBySize")
    public CommonResult getOrderByTypeLimitSize(String orderBy,Integer size){
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("order_no ","order_statue","order_real_price").orderBy(orderBy!=null && size!=null,true,orderBy).last("limit "+size);
        List<OrderSimpleVO> orderSimpleVOS = orderService.list(queryWrapper).stream().map(s -> new OrderSimpleVO(s.getOrderNo(),s.getOrderRealPrice(),s.getOrderStatue())).collect(Collectors.toList());
        return CommonResult.success(orderSimpleVOS);
    }

    @GetMapping("/saleLast7")
    public CommonResult getSaleForLast7(){
        List<Map<String,Object>> res = orderService.getSaleForLast7();
        return CommonResult.success(res);
    }

    @GetMapping("/countRoomRemain")
    public CommonResult getRoomRemain(){
        List<Map<String,Object>>res = roomService.getRoomRemain();
        return CommonResult.success(res);
    }

    @GetMapping("/remainRoom")
    public CommonResult getRemainRoom(){
        List<Map<String,Object>>res = roomService.getRemainRoom();
        return CommonResult.success(res);
    }
}
